package com.weipai.springboot.module.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weipai.springboot.model.Shop;
import com.weipai.springboot.model.ShopCategory;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.service.ShopCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2018/07/27 17:51
 * 
 * 店铺类别管理类
 */
@RestController
@RequestMapping("/shopCategory/")
public class ShopCategoryController {
    
    @Autowired
    ShopCategoryService shopCategpryService;

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页条数
     * @param draw 前台参数，照样返回chang
     * @param search 搜索，如果无搜索此值为paging
     * @param sort time
     * @param way desc，asc，noway
     *             
     */
    @GetMapping("/list/{page}/{size}/{draw}/{search}/{sort}/{way}")
    public Map<String, Object> list(@PathVariable("page") Integer page,
                                           @PathVariable("size") Integer size,
                                           @PathVariable("draw") String draw,
                                           @PathVariable("search") String search,
                                           @PathVariable("sort") String sort,
                                           @PathVariable("way") String way){
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(page, size);
        List<Shop> list = shopCategpryService.selectAllByShopCategoryDel(0,search, sort, way);
        PageInfo<Shop> pageInfo = new PageInfo<>(list);
        if(list.size()>0){
            map.put("pageData", pageInfo.getList());
            map.put("recordsTotal", pageInfo.getTotal());
            map.put("draw", draw);
            map.put("pages", pageInfo.getPages());
            map.put("status",200);
        }else{
            map.put("status",401);
            map.put("msg","暂无数据");
        }
        return map;
    }

    /**
     * 添加店铺类别
     * @param shopCategory
     * @return
     */
    @PostMapping("/save")
    public Response save(ShopCategory shopCategory){
        return shopCategpryService.save(shopCategory);
    }

    /**
     * 修改店铺类别
     * @param shopCategory
     * @return
     */
    @PutMapping("/update")
    public Response update(ShopCategory shopCategory){
        return shopCategpryService.update(shopCategory);
    }

    /**
     * 查询店铺类别详情
     * @param shopCategoryId:店铺类别的id
     * @return
     */
    @GetMapping("/getShopCategoryById/{id}")
    public Response getShopCategoryById(@PathVariable("id")  String shopCategoryId){
        return shopCategpryService.getShopCategoryById(shopCategoryId);
    }

    /**
     * 删除
     * @param shopCategoryId:店铺类别的id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    public Response deleteById(@PathVariable("id")  String shopCategoryId){
        /*
        删除店铺类别之前，首先判断这个类别是否有使用，如果有的话不可以
         */
        return shopCategpryService.deleteById(shopCategoryId);
    }

    /**
     * 更改店铺类别的状态
     * @param shopCategoryId:店铺类别的id
     * @return
     */
    @GetMapping("/changeStatus/{id}/{status}")
    public Response changeStatus(@PathVariable("id")  String shopCategoryId, 
                                            @PathVariable("status") Integer status){
        /*
        更改品牌之前，首先判断这个品牌是否有商品使用，如果有的话不可以
         */
        return shopCategpryService.changeStatus(shopCategoryId, status);
    }

    /**
     * 批量删除
     * @param id:店铺类别的id集合
     * @return
     */
    @DeleteMapping("/batchDeleteById/{id}")
    public Response batchDeleteById(@PathVariable("id")  String[] id){
        return shopCategpryService.batchDeleteById(id);
    }
    
}
